package com.wyw.core.config.support;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.sf.ehcache.Cache;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import com.wyw.core.config.XmlConfigParse;
import com.wyw.core.config.dto.CommDictDto;
import com.wyw.core.config.dto.CommDictOption;
import com.wyw.core.exception.WYWException;
import com.wyw.core.secu.dto.XmlConfigType;
import com.wyw.core.util.CacheManager;

public class CommDictParse implements XmlConfigParse{

	@Override
	public void parse(XmlConfigType xmlConfigType, Document xmlConfig) throws Exception {
		// TODO Auto-generated method stub
		//System.out.println("xmlConfigType:"+xmlConfigType.getXmlConfigType());
		List<Element> commDictDefineXmlList=xmlConfig.getRootElement().getChildren("dict");
		HashMap commDictConfigMap = new HashMap();
		CommDictDto commDictDto;
		String id;
		for(Element commDictXml:commDictDefineXmlList){
			id=commDictXml.getAttributeValue("id");
			if(commDictConfigMap.containsKey(id)){
				throw new WYWException("id："+id+" 数据字典重复加载");
			}else{
				commDictDto  = new CommDictDto();
				commDictDto.setId(id);
				commDictDto.setName(commDictXml.getAttributeValue("name"));
				parseDictOptions(commDictDto,commDictXml.getChildren("option"));
				commDictConfigMap.put(id, commDictDto);
			}
		}
		net.sf.ehcache.Element processDefinitionMap=new  net.sf.ehcache.Element(CacheManager.comm_dict_config_map,commDictConfigMap);
		CacheManager.put(processDefinitionMap);
	}
	
	private void parseDictOptions(CommDictDto commDictDto,List<Element> commDictXmlList){
		List options = new ArrayList();
		CommDictOption commDictOption ;
		for(Element  commDictXml:commDictXmlList){
			commDictOption = new CommDictOption();
			commDictOption.setCode(commDictXml.getAttributeValue("code"));
			commDictOption.setText(commDictXml.getAttributeValue("text"));
			options.add(commDictOption);
		}
		commDictDto.setOptions(options);
	}
		
}
